﻿using System;
using System.Collections.Generic;
using System.Text;

using Obsidian.Utils;
using Obsidian.Action;
using Oak.Base;
using Oak.Model;
using Oak.Core.Logic;

namespace Oak.Core.Serv
{
    public class ActCatServ : OakServ<OakSession>
    {

        public const string ART_CAT_FIELDS = "catId,parentId,catName,sortOrder,depth,path,modified,created,enabled";

        public ActCatServ(OakSession session) : base(session) { }

        public void List(ActionRequest req, ActionResponse res)
        {
            CoActCategoryQry qry = req.GetModelByNameOrFirst<CoActCategoryQry>("qry");
            string msg;
            ActCategoryLG catLG = new ActCategoryLG(Session);
            List<CoActCategoryInfo> list = catLG.GetActCatList(qry, out msg);
            if (list == null)
            {
                res.Error(msg);
                return;
            }
            ActionResult arItems = res.AddResult("cats", ART_CAT_FIELDS);
            arItems.AddModels(list);
        }

        public void Get(ActionRequest req, ActionResponse res)
        {
            ActReqParam param;
            if (!req.TryGetParam("cat", out param))
            {
                res.Error("参数cat错误");
                return;
            }
            long catId;
            if (!param.TryGetLong(0, "catId", out catId))
            {
                res.Error("传入属性catId错误");
                return;
            }
            ActCategoryLG catLG = new ActCategoryLG(Session);
            string msg;
            CoActCategoryInfo cat = catLG.GetArtCatById(catId, out msg);
            if (cat == null)
            {
                res.Error(msg);
                return;
            }

            ActionResult ar = res.AddResult("cats", ART_CAT_FIELDS);
            ar.AddModel(cat);
        }

        public void Add(ActionRequest req, ActionResponse res)
        {
            //判断登录
            if (!Session.IsAdmin)
            {
                res.SessionInvalid();
                return;
            }

            CoActCategoryInfo cat = req.GetModelByNameOrFirst<CoActCategoryInfo>("cat");
            ActCategoryLG catLG = new ActCategoryLG(Session);
            string msg;
            cat = catLG.AddArtCat(cat, out msg);
            if (cat == null)
            {
                res.Error(msg);
                return;
            }
            ActionResult ar = res.AddResult("cats", ART_CAT_FIELDS);
            ar.AddModel(cat);
        }

        public void Update(ActionRequest req, ActionResponse res)
        {
            //判断登录
            if (!Session.IsAdmin)
            {
                res.SessionInvalid();
                return;
            }

            CoActCategoryInfo cat = req.GetModel<CoActCategoryInfo>("cat");
            ActCategoryLG catLG = new ActCategoryLG(Session);
            string msg;
            cat = catLG.UpdateArtCat(cat, out msg);
            if (cat == null)
            {
                res.Error(msg);
                return;
            }
            ActionResult ar = res.AddResult("cats", ART_CAT_FIELDS);
            ar.AddModel(cat);
        }


        public void Paths(ActionRequest req, ActionResponse res)
        {
            ActReqParam param;
            if (!req.TryGetParam("cat", out param))
            {
                res.Error("cat参数错误");
                return;
            }
            string strCatIds;
            if (!param.TryGetString(0, "catIds", out strCatIds))
            {
                res.Error("catIds属性错误");
                return;
            }
            long[] catIds = ArrayUtil.SplitInt64(strCatIds, ',');
            ActCategoryLG catLG = new ActCategoryLG(Session);
            string msg;
            IList<CoActCategoryInfo> cats = catLG.GetActCatPaths(catIds, out msg);
            if (cats == null)
            {
                res.Error(msg);
                return;
            }
            ActionResult rsCats = res.AddResult("cats", ART_CAT_FIELDS);
            rsCats.AddModels(cats);
        }
    }
}
